<template>
  <view>
    <view class="container">
      <div style="height: 10vh;"></div>
      <div style="display: flex;align-items: center;margin-left: 2.5%;">
        <div class="leftArrow" @click="returnFeature"><u-icon color="#ffffff" size="28" name="arrow-left"></u-icon>
        </div>
        <div style="width: 10px;"></div>
        <div class="title" v-show="indent == '维保'">维保任务</div>
        <div class="title" v-show="indent == '巡检'">巡检任务</div>
        <div class="title" v-show="indent == '知识库'">知识库</div>
      </div>
      <div style="width: 95%; margin-left: 2.5%">
        <div style="display: flex;align-items: center;justify-content: space-between">
          <div style="color: #1a512c">
            <div style="height: 15rpx"></div>
            <div>您好！ ####，您加入该系统268天</div>
            <div style="height: 5rpx"></div>
            <div v-show="indent == '维保'">已累计处理{{ weibao.totalNum }}项维保任务</div>
            <div v-show="indent == '巡检'">已累计处理{{ xunjian.totalNum }}项巡检任务</div>
            <div v-show="indent == '知识库'">已累计处理{{ zhishiku.total }}项学习培训</div>
          </div>
          <!--  -->
          <div v-show="indent == '维保'" class="carStop" @click="carStop">
            <div class="stopIn">
              <div>设备</div>
              <div>报停</div>
            </div>
          </div>
          <div v-show="indent == '巡检'" class="carStop" @click="carStop">
            <div class="stopIn">
              <div>设备</div>
              <div>报修</div>
            </div>
          </div>
          <div v-show="indent == '知识库'" class="carStop" @click="carStop">
            <div class="stopIn">
              <div>生产</div>
              <div>报备</div>
            </div>
          </div>
          <!--  -->
        </div>
        <div style="height: 10px"></div>
        <!-- 边框 -->
        <view class="card">
          <div style="height: 20px"></div>
          <div v-if="indent != '知识库'" style="
              color: #1a512c;
              display: flex;
              align-items: center;
              justify-content: space-between;
              text-align: center;
            ">
            <div style="width: 33%">
              <div>
                <text style="font-size: 50px; font-weight: 900">
                  {{ indent == '维保' ? weibao.repairNum : indent == '巡检' ? xunjian.repairNum : '' }}
                </text>
              </div>
              <div>维修</div>
            </div>
            <div style="width: 33%">
              <text style="font-size: 50px; font-weight: 900">
                {{ indent == '维保' ? weibao.upkeepNum : indent == '巡检' ? xunjian.upkeepNum : '' }}
              </text>
              <div>保养</div>
            </div>
            <div style="width: 33%;text-align: center;">
              <text style="font-size: 50px; font-weight: 900">
                {{ indent == '维保' ? weibao.repairRate : indent == '巡检' ? xunjian.repairRate : '' }}
              </text>
              <div>
                {{ indent=='维保'?'维保':indent=='巡检'?'巡检率':'' }}
              </div>
            </div>
            <div style="width: 33%;">
              <!-- <qiun-data-charts 
              type="ring"
              :opts="optss"
              :chartData="chartDatas"
              /> -->
            </div>
          </div>
          <div v-if="indent == '知识库'" style="
              color: #1a512c;
              display: flex;
              align-items: center;
              justify-content: space-between;
              text-align: center;
            ">
            <div style="width: 33%">
              <div>
                <text style="font-size: 50px; font-weight: 900">
                  {{ zhishiku.product }}
                </text>
              </div>
              <div>生产</div>
            </div>
            <div style="width: 33%">
              <text style="font-size: 50px; font-weight: 900">
                {{ zhishiku.secure }}
              </text>
              <div>安全</div>
            </div>
            <div style="width: 33%;text-align: center;">
              <text style="font-size: 50px; font-weight: 900">
                {{ zhishiku.learningRatio }}
              </text>
              <div>培训学习</div>
            </div>
            <div style="width: 33%;">
              <!-- <qiun-data-charts 
              type="ring"
              :opts="optss"
              :chartData="chartDatas"
              /> -->
            </div>
          </div>
          <div style="height: 20px"></div>
        </view>
        <div style="height: 10px"></div>
        <div>
          <div v-if="indent == '维保'" style="color: #1a512c;">
            已完成处理{{ weibao.finishNum }}项，处理中{{ weibao.ingNum }}项，待处理{{ weibao.pendingNum }}项</div>
          <div v-if="indent == '巡检'" style="color: #1a512c;">
            已完成处理{{ xunjian.finishNum }}项，处理中{{ xunjian.ingNum }}项，待处理{{ xunjian.pendingNum }}项</div>
          <div v-if="indent == '知识库'" style="color: #1a512c;">已完成处理{{ zhishiku.completed }}项，待处理{{ zhishiku.undone }}项</div>
          <div style="height: 10px"></div>
        </div>
        <!--  -->
        <div class="center-bg" v-if="indent != '知识库'">
          <div style="height: 15px"></div>
          <div style="width: 90%; margin-left: 5%">
            <div>
              <div v-if="weibao.taskDes!=''||xunjian.taskDes!=''">
                {{ indent == '维保' ? weibao.taskDes : indent == '巡检' ? xunjian.taskDes : '' }}
              </div>
              <div v-if="weibao.taskDes==''||xunjian.taskDes==''">
                暂无
              </div>
            </div>
          </div>
          <div style="height: 15px"></div>
        </div>
        <div style="height: 10px"></div>
        <div style="border-radius: 10px">
          <div style="height: 10px"></div>
          <!-- eCharts -->
          <view class="charts-box">
            <div style="display: flex;">
              <div v-for="(item, index) in week" style="margin: 0 10rpx 0 0;" @click="toggleIndex(item, index)">
                <div class="roundDate" :class="index == indexs ? 'active' : ''">{{ item }}</div>
              </div>
            </div>
            <qiun-data-charts type="column" :opts="opts" :chartData="chartData" />
          </view>
          <div style="height: 10px"></div>
        </div>
        <!-- 底部 -->
        <div style="height: 10px"></div>
        <div class="foot-card">
          <div style="height: 15px"></div>
          <div style="
              display: flex;
              align-items: center;
              justify-content: space-between;
              width: 95%;
              margin-left: 2.5%;
            ">
            <div>
              <div style="color: #1a512c; font-weight: 500">
                <div v-show="indent == '维保'">您当前有{{ weibao.pendingNum }}项维保任务待处理</div>
                <div v-show="indent == '巡检'">您当前有{{ xunjian.pendingNum }}项巡检任务待处理</div>
                <div v-show="indent == '知识库'">您当前有1项安全培训待处理</div>
              </div>
              <div v-show="indent == '维保'" style="color: #c5b46d; font-size: 14px; font-weight: 500">
                今日累计完成{{ weibao.finishNum }}项维保任务
              </div>
              <div v-show="indent == '巡检'" style="color: #c5b46d; font-size: 14px; font-weight: 500">
                今日累计完成{{ xunjian.finishNum }}项维保任务
              </div>
              <div v-show="indent == '知识库'" style="color: #c5b46d; font-size: 14px; font-weight: 500">
                今日累计完成{{doneFinish}}项巡培训学习
              </div>
            </div>
            <div class="look" @click="looks">查看</div>
          </div>
          <div style="height: 15px"></div>
        </div>
        <div style="height: 5vh;"></div>
      </div>
    </view>
    <tabBar :currentPage="currentPage" :role="role" :indent="indent"></tabBar>
  </view>
</template>
<script>
import qiunDataCharts from '@/pagesScreen/subpackageuni/qiun-data-charts/components/qiun-data-charts/qiun-data-charts.vue'
import tabBar from '../../compontents/tabBar/index.vue';
import { repairNum, insNum, indexIns, indexMain, knowNum, indexKnow,finishTestNum } from '../api/api.js';
export default {
  components: {
    tabBar,
    qiunDataCharts
  },
  data() {
    return {
      doneFinish:0,
      indexs: 0,
      week: ['周', '月', '年'],
      weibao: {},
      xunjian: {},
      zhishiku: {},
      userInfo: {},
      flag: false,
      role: '',//司机/养殖
      chartData: {},
      chartDatas: {},
      currentPage: '',
      opts: {
        color: [
          "#1890FF",
          "#91CB74",
          "#FAC858",
          "#EE6666",
          "#73C0DE",
          "#3CA272",
          "#FC8452",
          "#9A60B4",
          "#ea7ccc",
        ],
        padding: [15, 15, 0, 5],
        enableScroll: false,
        legend: {},
        xAxis: {
          disableGrid: true,
        },
        yAxis: {
          data: [
            {
              min: 0,
            },
          ],
        },
        extra: {
          column: {
            type: "group",
            width: 30,
            activeBgColor: "#000000",
            activeBgOpacity: 0.08,
          },
        },
      },
      mainParam: ''
    };
  },
  methods: {
    toggleIndex(i, index) {
      console.log(i, index);

      if (this.indent == '维保') {
        if (i == '周') {
          this.mainChart(7)
          this.indexs = index;
        } else if (i == '月') {
          this.mainChart(30)
          this.indexs = index;
        } else if (i == '年') {
          this.mainChart(365)
          this.indexs = index;
        }
      } else if (this.indent == '巡检') {
        if (i == '周') {
          this.insChart(7)
          this.indexs = index;
        } else if (i == '月') {
          this.insChart(30)
          this.indexs = index;
        } else if (i == '年') {
          this.insChart(365)
          this.indexs = index;
        }
      } else if (this.indent == '知识库') {
        if (i == '周') {
          this.knowChart(7)
          this.indexs = index;
        } else if (i == '月') {
          this.knowChart(30)
          this.indexs = index;
        } else if (i == '年') {
          this.knowChart(365)
          this.indexs = index;
        }
      }

      // this.chartsFarm(i);
    },
    // 缴费
    fee() {
      uni.navigateTo({ url: '/pagesFarmer/index/fees' })
    },
    // 查看工单
    looks() {
      if (this.indent == '知识库') {
        uni.navigateTo({
          url: '/pagesPlant/index/save'
        })
      } else if (this.indent == '维保' || this.indent == '巡检') {
        if(this.indent=='维保'&&this.weibao.pendingNum<=0){
          uni.showToast({
            title: '暂无工单',
            icon:'none'
          });
        }else if(this.indent=='巡检'&&this.xunjian.pendingNum<=0){
          uni.showToast({
            title: '暂无工单',
            icon:'none'
          });
        }else{
          uni.navigateTo({ url: '/pagesPlant/index/procedetail' })
        }
        
      } else {
        uni.navigateTo({ url: '/pagesDriver/system/workOrder' })
      }
    },
    //   提交
    submit() {
      uni.navigateTo({ url: '/pagesDriver/system/filling' })
    },
    // 管理
    manage() {

    },
    returnFeature() {
      uni.redirectTo({
        url: '/pagesPlant/index/feature'
      })
    },
    carStop() {
      uni.navigateTo({ url: '/pagesPlant/index/outage' })
    },
    //维保图表
    mainChart(i) {
      indexMain(i).then(rese => {
        console.log(rese.data.data, '维保图表');
        const week = rese.data.data.map((item) => item.Field)
        const value = rese.data.data.map((item) => item.count);
        let res = {
          categories: week,
          series: [
            {
              name: '量',
              data: value
            }
          ]
        }
        this.chartData = JSON.parse(JSON.stringify(res));
      })
    },
    insChart(i) {
      indexIns(i).then(rese => {
        console.log(rese.data, '巡检图表');
        const week = rese.data.data.map((item) => item.Field)
        const value = rese.data.data.map((item) => item.count);
        let res = {
          categories: week,
          series: [
            {
              name: '量',
              data: value
            }
          ]
        }
        this.chartData = JSON.parse(JSON.stringify(res));
      })
    },
    knowChart(i) {
      indexKnow(i).then(rese => {
        console.log(rese.data, '知识库图表');
        const week = rese.data.data.map((item) => item.Field)
        const value = rese.data.data.map((item) => item.count);
        let res = {
          categories: week,
          series: [
            {
              name: '量',
              data: value
            }
          ]
        }
        this.chartData = JSON.parse(JSON.stringify(res));
      })
    }
  },
  onShow() {
    uni.hideHomeButton();
    // this.role = uni.getStorageSync('userInfo').remark;
    this.indent = uni.getStorageSync('pageIndent');
    this.role = uni.getStorageSync('userInfo').role;
    //维保数量
    if (this.indent == '维保') {
      repairNum().then(res => {
        console.log(res.data, '维保首页');
        if (res.data.code == 200) {
          this.weibao = res.data.data;
          // let valueFloat = parseFloat(this.weibao.repairRate.replace('%', ''));
          // let valueFormatted = valueFloat.toFixed(1);
          // this.weibao.repairRate = valueFormatted+'%';
        }
      })
      this.mainChart(7);
    } else if (this.indent == '巡检') {
      insNum().then(res => {
        console.log(res.data, '巡检首页');
        if (res.data.code == 200) {
          this.xunjian = res.data.data;
          // let valueFloat = parseFloat(this.xunjian.repairRate.replace('%', ''));
          // let valueFormatted = valueFloat.toFixed(1);
          // this.xunjian.repairRate = valueFormatted+'%';
        }
      })
      this.insChart(7);
    } else if (this.indent == '知识库') {
      knowNum().then(res => {
        console.log(res.data, '知识库首页');
        if (res.data.code == 200) {
          this.zhishiku = res.data.data;
        }
      })
      finishTestNum({
        taskState:0
      }).then(res=>{
        if(res.data.code==200){
          this.doneFinish = res.data.data || 0;
        }
      })
      this.knowChart(7);
    }

  },

  onLoad() {
    var pages = getCurrentPages();
    let currentPageRoute = pages[pages.length - 1].route;
    // 根据当前页面路径设置currentPage
    this.currentPage = currentPageRoute;
    console.log(this.currentPage);
  }
};
</script>
<style lang="scss">
page {
  background-color: #f6f3e7;
}

.container {
  // background: #f6f3e7;
  height: 100%;
  max-height: 91vh;
  overflow-y: scroll;
  // position: absolute;
  // left: 0;
  width: 100vw;
}

.carStop {
  display: flex;
  justify-content: center;
  align-items: center;
  background: green;
  padding-right: 10px;
  padding-left: 10px;
  padding-top: 5px;
  padding-bottom: 5px;
  border-radius: 50%;
  color: #fff;
}

.stopIn {}

.card {
  // border: 3px solid #1a512c;s
  border-radius: 10px;
}

.center-bg {
  background: #1a512c;
  color: #fff;
  // width: 95%;
  // margin-left: 2.5%;
  border-radius: 10px;
}

.charts-box {
  width: 100%;
  height: 230px;
}

.foot-card {
  background: #fff;
  border-radius: 10px;
}

.look {
  background: #1a512c;
  color: #fff;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.looks {
  background: #D0463F;
  color: #fff;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tabBar {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100vw;
  height: 9vh;
}

.leftArrow {
  background: green;
  border-radius: 5px;
  padding-top: 5px;
  padding-bottom: 5px;
  padding-left: 10px;
  padding-right: 10px;
}

.title {
  font-size: 24px;
  color: #1A512C;
  font-weight: 700;
}

.roundDate {
  border-radius: 50%;
  border: 1px solid #1A512C;
  height: 50rpx;
  width: 50rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1A512C;
  background-color: #F6F3E7
}

.active {
  background: #1A512C;
  color: #fff;
}
</style>